package com.lion.tag.tree;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.lion.tag.LionBaseTag;
import com.lion.tag.util.AppendUtil;

public class Tree extends LionBaseTag {
	private static final long serialVersionUID = 6997793058612316413L;
	private String url;
	private String method;// 请求类型(get|post)
	private String animate;// 滑动效果
	private String checkbox;// 复选框
	private String onContextMenu;// 右键菜单控件id
	private String lines;// 显示线条

	@Override
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder(200);
		sb.append("<ul ");
		sb = AppendUtil.addAttribute(sb, "id", id);
		sb = AppendUtil.addAttribute(sb, "class", "easyui-tree");
		int hasData = 0;
		hasData = AppendUtil.appendOptionData(sb, hasData, "url", getContextPath()+url, true);
		hasData = AppendUtil.appendOptionData(sb, hasData, "method", method, true);
		hasData = AppendUtil.appendOptionData(sb, hasData, "animate", animate, false);
		hasData = AppendUtil.appendOptionData(sb, hasData, "checkbox", checkbox, false);
		hasData = AppendUtil.appendOptionData(sb, hasData, "lines", lines, false);
		if (onContextMenu != null && !onContextMenu.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("onContextMenu:");
			sb
					.append("function(e,node){e.preventDefault();$(this).tree('select',node.target);$('#");
			sb.append(onContextMenu);
			sb.append("').menu('show',{left: e.pageX,top: e.pageY});}");
		}
		if(hasData==1){
			sb.append("\">");
		}else{
			sb.append(">");
		}
		try {
			pageContext.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;// 表示标签之间的内容被正常执行
	}

	@Override
	/**  
	 *  doEndTag：当JSP容器遇到自定义标签的结束标志，就会调用doEndTag()方法。doEndTag()方法也返回一个整数值，用来决定程序后续流程。  
	 *  A.Tag.SKIP_PAGE：表示立刻停止执行网页，网页上未处理的静态内容和JSP程序均被忽略任何已有的输出内容立刻返回到客户的浏览器上。  
	 *  B.Tag.EVAL_PAGE：表示按照正常的流程继续执行JSP网页  
	 */
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("</ul>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method
	 *            the method to set
	 */
	public void setMethod(String method) {
		this.method = method;
	}

	/**
	 * @return the animate
	 */
	public String getAnimate() {
		return animate;
	}

	/**
	 * @param animate
	 *            the animate to set
	 */
	public void setAnimate(String animate) {
		this.animate = animate;
	}

	/**
	 * @return the checkbox
	 */
	public String getCheckbox() {
		return checkbox;
	}

	/**
	 * @param checkbox
	 *            the checkbox to set
	 */
	public void setCheckbox(String checkbox) {
		this.checkbox = checkbox;
	}

	/**
	 * @return the onContextMenu
	 */
	public String getOnContextMenu() {
		return onContextMenu;
	}

	/**
	 * @param onContextMenu
	 *            the onContextMenu to set
	 */
	public void setOnContextMenu(String onContextMenu) {
		this.onContextMenu = onContextMenu;
	}

	/**
	 * @return the lines
	 */
	public String getLines() {
		return lines;
	}

	/**
	 * @param lines
	 *            the lines to set
	 */
	public void setLines(String lines) {
		this.lines = lines;
	}

}
